package ro.msg.quiz.Connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author Blaj This is the class responsible for the connection to the database
 * 
 */
public class ConnectJdbc {
	// JDBC driver name and database URL
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	static final String DB_URL = "jdbc:mysql://localhost/quizappdb";

	// Database credentials
	static final String USER = "root";
	static final String PASS = "root";

	/**
	 * Try to establish a connection to the database
	 * 
	 * @return a type Connection if the connection has succeded
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			// STEP 2: Register JDBC driver
			Class.forName("com.mysql.jdbc.Driver");

			// STEP 3: Open a connection

			conn = DriverManager.getConnection(DB_URL, USER, PASS);

			return conn;
		} catch (SQLException se) {
			// Handle errors for JDBC
			se.printStackTrace();
		} catch (Exception e) {
			// Handle errors for Class.forName
			e.printStackTrace();
		}
		return null;
	}// end main
}// end FirstExample
